package edu.hawaii.duedates.book;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;

/**
 * This will test the Book class.
 * 
 * @author Ronn Reeves and Robin E. Raqueno
 *
 */
public class TestBook {

  /**
   * Test creating a book.
   */
  @Test 
  public void testCreateBook() {
    String title = "Title";
    String date = "10/20/2008";
    String library = "Test library";
    Book book = new Book(title, date, library);
    book.setDueDate(date);
    book.setLibrary(library);
    book.setTitle(title);
   
    assertEquals("compareByLibrary() test failed.", book.compareByLibrary(book), 0);
    assertEquals("toString() test failed.", book.toString(), book.getTitle());
    assertEquals("getTitle() test failed.", title, book.getTitle());
    assertEquals("getDueDate() test failed.", "10/20/08", book.convertDate());
    assertEquals("getLibrary() test failed.", library, book.getLibrary()); 
    assertEquals("compareByDate() test failed.", book.compareByDate(book), 0);
  }

  
  /**
   * Test the formatString and formatDate.
   */
  @Test 
  public void testTitleFormat() {
    String longTitle = "TitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitle" 
      + "TitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitleTitle";
    String dueDate = "Checked Out: 12/30/08 11:59";
    String library = "Kaimuki Library";
    String failMessage = "getTitle() test format failed.";
      
    String bookTitle1 = longTitle;
    Book book1 = new Book(bookTitle1, dueDate, library);
    
    assertEquals(failMessage, book1.toString(), book1.getTitle());
    assertEquals("getDueDate() test format failed.", book1.getDueDate(), book1.getDueDate());
    
    String frontTrim = "TRIM_FROM_FRONT  ";
    String endTrim1 = "/TRIM_FROM_END";
    String endTrim2 = " Location:_TRIM_FROM_END";

    String bookTitle2 = frontTrim + longTitle + endTrim1 + endTrim2;
    Book book2 = new Book(bookTitle2, dueDate, library);

    assertEquals(failMessage, book2.toString(), book2.getTitle());

    String bookTitle3 = "Location:" + longTitle + '/' +  "  " + endTrim1;
    Book book3 = new Book(bookTitle3, dueDate, library);
   
    assertEquals(failMessage, book3.toString(), book3.getTitle());  

    String bookTitle4 = frontTrim + " " + longTitle + endTrim2;
    Book book4 = new Book(bookTitle4, dueDate, library);

    assertEquals(failMessage, book4.toString(), book4.getTitle());

    frontTrim = "Location:TRIM_FROM_FRONT  ";
    String bookTitle5 = frontTrim + longTitle;
    Book book5 = new Book(bookTitle5, dueDate, library);
    System.out.println(book5.getTitle());
    
    assertEquals(failMessage, book5.toString(), book5.getTitle());
  }
  
  
  /**
   * Test the formatDate method.
   */
  @Test (expected = NumberFormatException.class)
  public void testDateFormat() {
    String title = "Title";
    String date = "August 10, 2008";
    String library = "Test library";
    new Book(title, date, library);
    fail("compareByDate() test failed");
    
  }
}